package com.gu.sync;

/**
 * 售货员卖票类
 *
 * @author gusongliao
 * @date 2023-05-18 16:10
 */
public class SaleTicket {

    public static void main(String[] args) {
        // 创建Ticket对象
        Ticket ticket = new Ticket();
        // 创建三个线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.sale();
                }
            }
        }, "AA").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.sale();
                }
            }
        }, "BB").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.sale();
                }
            }
        }, "CC").start();
        System.out.println("------------------");

    }

}

class Ticket {
    // 票的数量
    private int number = 90;

    public synchronized void sale() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "：卖出：" + (number--) + "：剩余：" + (number)) ;
        }
    }

}


